package com.zprmb.udzt;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.NumUtil;
import com.zprmb.udzt.util.Regular;
import com.zprmb.udzt.util.ToJSON;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.util.VoteCalculator;
import com.zprmb.udzt.view.AddAndSubView;
import com.zprmb.udzt.view.AddAndSubView.OnNumChangeListener;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;

public class V6BbtinvestActivity extends BaseActivity implements
		OnClickListener {
	private AddAndSubView addAndSubView;
	private RelativeLayout rl_back;
	private TextView tv_title;
	private TextView tv_availableAmount, tv_dexterityyue,
			tv_product_remainamount;
	private TextView tv_min_increaseNum;
	private String name, type, loanid;
	private RelativeLayout rl_use_coupon;
	private String reward_id;// 礼券id
	private DecimalFormat df = new DecimalFormat("###0.00");
	private String red_type = ""; // 红包类型
	private int ischange = 0;
	private Double availableGiftAmount = 0.0;// 用户红包总量
	private Double coupon_rate = 0.0, red_rate = 0.0;// 加息券利率 红包利率
	private TextView coupon_num;// 加息券所剩张数
	private TextView coupon_name;// 加息券名字
	private int day;
	private Double rate;
	private Double rate_add;
	private CheckBox check_dexterity;
	private TextView tv_add_money;
	private boolean use_status = false;
	private SharedPreferences sha;
	private Double availableAmount = 0.0;
	private TextView tv_product_shouyi;
	int minAmount;
	private LinearLayout li_tishi;
	private DecimalFormat df2 = new DecimalFormat("###0");
	private int maxAmount;
	int remainAmount;
	private boolean payPass = true;
	private Double curAmount = 0.0, maxcurAmount = 0.0;
	private boolean nameauth = true, islogin = true;
	protected int increaseNum;
	private boolean ishuo;
	protected boolean tishi_coupon = true;
	protected boolean resultcoupon = false;
	private TextView tv_tishi, check_mAgreement;
	private Button btn_invest;
	private String flagFresh;
	private RelativeLayout rl_dexterity;
	private ImageView img_ck_status;
	private int useFundAmountInvestFlag = 1;// 特权时间
	// private Double proportion;//项目利息
	private CheckBox check;
	private TextView tv_mAgreement;
	private boolean flagred;
	private int month;
	private String repaytype;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v6_investbbt_activity);
		setContentInview(V6BbtinvestActivity.this)
				.setVisibility(View.INVISIBLE);
		initIntent();
		BbtApplication.getInstance().addActvity(this);
		tv_title = (TextView) findViewById(R.id.tv_title);
		rl_back = (RelativeLayout) findViewById(R.id.rl_back);
		initView();
		rl_back.setOnClickListener(this);
		inintData(1);
		Toast.makeText(this, "投资啊", Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		inintData(0);
		userinfo();
	}

	private void myconmment() {
		if (useFundAmountInvestFlag == 0) {

			if (addAndSubView.getNum() != 0
					&& addAndSubView.getNum() > curAmount && islogin
					&& nameauth) {
				if (curAmount == 0) {

					li_tishi.setVisibility(View.VISIBLE);
					tv_tishi.setText("特权时间过后即可投资");
				} else {
					if (addAndSubView.getNum() != 0
							&& addAndSubView.getNum() > maxcurAmount) {
						li_tishi.setVisibility(View.VISIBLE);
						tv_tishi.setText("智投宝转投单笔不超过"
								+ (df2.format(maxcurAmount / 10000)) + "万元");

					} else {
						li_tishi.setVisibility(View.VISIBLE);
						if (addAndSubView.getNum() != 0
								&& addAndSubView.getNum() > curAmount) {
							tv_tishi.setText("智投宝余额不足");

						} else {

							investComment();
						}
					}

				}
			} else {
				if (addAndSubView.getNum() != 0
						&& addAndSubView.getNum() > maxcurAmount) {
					li_tishi.setVisibility(View.VISIBLE);
					tv_tishi.setText("智投宝转投单笔不超过"
							+ (df2.format(maxcurAmount / 10000)) + "万元");

				} else {
					li_tishi.setVisibility(View.VISIBLE);
					if (addAndSubView.getNum() != 0
							&& addAndSubView.getNum() > curAmount) {
						tv_tishi.setText("智投宝余额不足");

					} else {

						investComment();
					}
				}
			}
			changeStatus();

		} else {
			if (ishuo) {
				if (addAndSubView.getNum() != 0
						&& ishuo
						&& addAndSubView.getNum() > (curAmount + availableAmount)
						&& islogin && nameauth) {
					if (addAndSubView.getNum() != 0
							&& (addAndSubView.getNum() - availableAmount) > maxcurAmount
							&& curAmount >= maxcurAmount) {
						li_tishi.setVisibility(View.VISIBLE);
						tv_tishi.setText("智投宝转投单笔不能超过"
								+ df2.format(maxcurAmount / 10000) + "万元");
					} else {
						li_tishi.setVisibility(View.VISIBLE);
						tv_tishi.setText("账户可用余额不足,请充值!");

					}

				} else {
					if (addAndSubView.getNum() != 0
							&& (addAndSubView.getNum() - availableAmount) > maxcurAmount) {
						li_tishi.setVisibility(View.VISIBLE);
						tv_tishi.setText("智投宝转投单笔不能超过"
								+ df2.format(maxcurAmount / 10000) + "万元");
					} else {
						investComment();
					}

				}
			} else {
				if (addAndSubView.getNum() != 0
						&& availableAmount < addAndSubView.getNum()
						&& (ishuo == false) && islogin && nameauth) {
					if (curAmount > 0) {
						li_tishi.setVisibility(View.VISIBLE);
						tv_tishi.setText("可用余额不足，请充值或选择智投宝余额转投");
						Log.e("提示", "可用余额不足，请充值或选择智投宝余额转投");
					} else {
						li_tishi.setVisibility(View.VISIBLE);
						tv_tishi.setText("账户可用余额不足,请充值!");

					}

				} else {
					investComment();
				}
			}
		}
		changeStatus();
	}

	private void investComment() {
		if (increaseNum != 0) {

			if (minAmount > remainAmount) {
				// if((Integer.valueOf(et_money.getText().toString())!=remainAmount)){
				// ToastUtil.showCenterToast(DetailsActivity.this,
				// "投资金额的增量要为" + increaseNum + "的倍数");
				// return;
				// }
				li_tishi.setVisibility(View.GONE);
			} else {
				if ((addAndSubView.getNum() - minAmount) % increaseNum != 0) {
					li_tishi.setVisibility(View.VISIBLE);
					tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
					// ToastUtil.showCenterToast(V6investActivity.this,
					// "投资金额的增量要为" + increaseNum + "的倍数");

				} else {
					li_tishi.setVisibility(View.GONE);
				}
			}
		}
		if (maxAmount != 0) {
			if (addAndSubView.getNum() > maxAmount) {
				li_tishi.setVisibility(View.VISIBLE);
				tv_tishi.setText("最大投资金额为" + maxAmount + "元");

			} else {
				if ((addAndSubView.getNum() - minAmount) % increaseNum != 0) {

				} else {

					li_tishi.setVisibility(View.GONE);
				}
			}
		}
		if (remainAmount != 0) {
			if (addAndSubView.getNum() > remainAmount) {

				li_tishi.setVisibility(View.VISIBLE);
				tv_tishi.setText("投资金额不能大于剩余可投金额！");
			} else {
				if ((addAndSubView.getNum() - minAmount) % increaseNum != 0
						|| addAndSubView.getNum() > maxAmount) {

				} else {

					li_tishi.setVisibility(View.GONE);
				}
			}
		}
	}

	private void inintData(final int creat) {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				setContentInview(V6BbtinvestActivity.this).setVisibility(
						View.VISIBLE);
				JSONObject json1 = jsonObject.optJSONObject("data");
				if (jsonObject.optInt("success") == 1) {
					// progressLayout.showContent();
					LogUtils.e(V6BbtinvestActivity.this, jsonObject.toString());
					ToJSON.initData2(jsonObject.toString(), "san");
					JSONObject json2 = json1.optJSONObject("loanRequest");
					JSONObject json3 = json1.optJSONObject("loan");
					// ToJSON.initData2(jsonObject.toString(), "bbt");

					if (json1 != null) {
						JSONObject user = json1.optJSONObject("user");
						JSONObject jsoncoupon = json1
								.optJSONObject("couponInfo");
						repaytype = json2.optString("requestMethod");
						if (user != null) {
							islogin = true;
							check_dexterity.setEnabled(true);
							rl_dexterity.setEnabled(true);
							tv_availableAmount.setText(Regular
									.getDecimalFormatTwo(user
											.optDouble("availableAmount")));
							tv_dexterityyue.setText(Regular
									.getDecimalFormatTwo(user
											.optDouble("curAmount")));
							// availableGiftAmount=user.optDouble("availableGiftAmount ");
							availableAmount = user.optDouble("availableAmount");
							curAmount = user.optDouble("curAmount");
							maxcurAmount = user
									.optDouble("maxTransferInvestAmount");
							useFundAmountInvestFlag = user
									.optInt("useFundAmountInvestFlag");
						} else {
							check_dexterity.setEnabled(false);
							rl_dexterity.setEnabled(false);
							islogin = false;
							tv_availableAmount.setText("0.00");
							tv_dexterityyue.setText("0.00");
							coupon_name.setText("登录查看");
							coupon_num.setText("");
						}
						Log.e("num", creat + "");

						if (jsoncoupon != null) {

							if (ischange == 2
									&& !sha.getString("check", "").equals("")) {

							} else {

								coupon_num.setText(jsoncoupon
										.optString("couponCount") + "张");
								coupon_name.setText(jsoncoupon
										.optString("couponMsg"));
							}
						}
						rate = (json2.optDouble("rate")) / 100d;
						if (repaytype.equals("EqualInstallment")) {
							if (json1.optInt("year") == 0) {
								month = json1.optInt("month");
							} else {
								month = json1.optInt("month")
										+ json1.optInt("year") * 12;
							}
						} else if (repaytype.equals("EqualPrincipal")) {
							// 按月等额本金
							if (json1.optInt("year") == 0) {
								month = json1.optInt("month");
							} else {
								month = json1.optInt("month")
										+ json1.optInt("year") * 12;
							}
						}
						day = json1.optInt("loanRealDays");
						// day = json1.optInt("loanRealDays");
						tv_product_remainamount.setText(NumUtil
								.getQiantwo(json1.optString("remainAmount"))
								+ "元");
						increaseNum = json1.optInt("stepAmount");
						tv_min_increaseNum.setText(json1.optString("minAmount")
								+ "元起投," + json1.optString("stepAmount")
								+ "元倍数递增");

						if (creat == 1) {
							addAndSubView.setNum(json1.optInt("minAmount"));
						}
						// addAndSubView.setMinnum(json1.optInt("minAmount"));
						addAndSubView
								.setincreaseNum(json1.optInt("stepAmount"));
					}
				}
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(V6BbtinvestActivity.this, Constant.URL
				+ "/apiLoan/loan/" + loanid, "");
		Log.e("bbturl", Constant.URL + "/apiLoan/loan/" + loanid);
	}

	private void initView() {
		// TODO Auto-generated method stub
		addAndSubView = (AddAndSubView) findViewById(R.id.add_subview);
		addAndSubView.setEditTextLayoutWidth(800);
		addAndSubView.setNum(0);
		if (getScreenWidth() > 1080) {
			addAndSubView.setButtonLayoutParm(100, 100);
		} else {
			addAndSubView.setButtonLayoutParm(60, 60);
		}
		tv_mAgreement = (TextView) findViewById(R.id.tv_mAgreement);
		check_mAgreement = (TextView) findViewById(R.id.check_mAgreement);
		check = (CheckBox) findViewById(R.id.check);
		rl_dexterity = (RelativeLayout) findViewById(R.id.rl_dexterity);
		sha = getSharedPreferences("coupon", MODE_PRIVATE);
		img_ck_status = (ImageView) findViewById(R.id.ck_status);
		li_tishi = (LinearLayout) findViewById(R.id.li_tishi);
		tv_product_shouyi = (TextView) findViewById(R.id.tv_product_shouyi);
		tv_tishi = (TextView) findViewById(R.id.tv_tishi);
		addAndSubView
				.setButtonBgResource(R.drawable.jiahao, R.drawable.jianhao);
		tv_availableAmount = (TextView) findViewById(R.id.tv_availableAmount);
		tv_dexterityyue = (TextView) findViewById(R.id.tv_dexterityyue);
		check_dexterity = (CheckBox) findViewById(R.id.check_dexterity);
		tv_product_remainamount = (TextView) findViewById(R.id.tv_product_remainamount);
		tv_min_increaseNum = (TextView) findViewById(R.id.tv_min_increaseNum);
		rl_use_coupon = (RelativeLayout) findViewById(R.id.rl_use_coupon);
		coupon_num = (TextView) findViewById(R.id.coupon_num);
		coupon_name = (TextView) findViewById(R.id.v6_coupon_name);
		tv_add_money = (TextView) findViewById(R.id.tv_add_money);
		btn_invest = (Button) findViewById(R.id.btn_invest);
		tv_mAgreement.setOnClickListener(this);
		btn_invest.setOnClickListener(this);
		tv_add_money.setOnClickListener(this);
		rl_use_coupon.setOnClickListener(this);
		rl_dexterity.setOnClickListener(this);
		check_mAgreement.setOnClickListener(this);
		check_dexterity
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							img_ck_status.setVisibility(View.VISIBLE);
							check_dexterity.setChecked(true);
						} else {
							img_ck_status.setVisibility(View.GONE);
							check_dexterity.setChecked(false);
						}
						ishuo = isChecked;
						myconmment();
					}
				});

		check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					btn_invest.setEnabled(true);
					btn_invest.setBackgroundResource(R.drawable.v6button);
				} else {
					btn_invest.setEnabled(false);
					btn_invest.setBackgroundResource(R.drawable.btn_unclick);
				}
			}
		});

		addAndSubView.setOnNumChangeListener(new OnNumChangeListener() {

			@Override
			public void onNumChange(View view, int num) {
				// TODO Auto-generated method stub

				myconmment();
				product_ShouYi();
				if (ischange == 2) {
					if (use_status) {

						interest();
					} else {
						incoupon();
					}
				} else {

				}

			}
		});
		tv_title.setText(name);
	}

	private void initIntent() {
		// TODO Auto-generated method stub
		name = getIntent().getStringExtra("name");
		type = getIntent().getStringExtra("type");
		loanid = getIntent().getStringExtra("id");
	}

	private void userinfo() {
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if (jsonObject.optInt("success") == 1) {
					JSONObject json1 = jsonObject.optJSONObject("data");
					islogin = true;
					if (json1 != null) {

						tv_availableAmount.setText(Regular
								.getDecimalFormatTwo(json1
										.optDouble("availableAmount")));
						// tv_dexterityyue.setText(Regular.getDecimalFormatTwo(json1.optDouble("ltbAmount")));
						// availableGiftAmount=json1.optDouble("availableGiftAmount ");
						availableAmount = json1.optDouble("availableAmount");

					}
				} else {
					islogin = false;
					tv_availableAmount.setText("0.00");

				}
				LogUtils.e(V6BbtinvestActivity.this, jsonObject.toString());
				// ToJSON.initData2(jsonObject.toString(),"userinfo" );
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(false);
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.execute(V6BbtinvestActivity.this, Constant.URL
				+ "/api/v5/user/accountCenter", "");
	}

	private void interest() {
		Log.e("计息", "开始计算");
		if (!red_type.equals("")) {
			if (addAndSubView.getNum() != 0) {
				if (red_type.equals("PERCENTAGE")) {
					double redpage = addAndSubView.getNum() * (0.005);
					if (redpage > availableGiftAmount) {
						redpage = availableGiftAmount;
					}
					coupon_num.setText(df.format(redpage) + "元");
				} else {
					Log.e("num", addAndSubView.getNum() + "----" + day + "---"
							+ addAndSubView.getNum() * (red_rate / 100) / 365
							* day + "   " + red_rate);
					// if(repaytype.equals("EqualInstallment")){
					// //按月等额本息
					// // double redpage = addAndSubView.getNum() *
					// (red_rate/100)/365*day;
					// String redpage =
					// VoteCalculator.equalprincipalandinterest(new
					// BigDecimal(""+addAndSubView.getNum()), new
					// BigDecimal(""+red_rate/100).divide(new BigDecimal("12"),
					// MathContext.DECIMAL128), day);
					// if (Double.parseDouble(redpage) > availableGiftAmount) {
					// redpage = String.valueOf(availableGiftAmount);
					// }
					// coupon_num.setText(redpage
					// + "元");
					// }else if(repaytype.equals("EqualPrincipal")){
					// //按月等额本金
					// String
					// redpage=VoteCalculator.matchingprincipal(Double.valueOf(addAndSubView.getNum()),
					// red_rate, day);
					// if (Double.parseDouble(redpage) > availableGiftAmount) {
					// redpage = String.valueOf(availableGiftAmount);
					// }
					// coupon_num.setText(redpage
					// + "元");
					// }
					// else{
					double redpage = addAndSubView.getNum() * (red_rate / 100)
							/ 365 * day;
					if (redpage > availableGiftAmount) {
						redpage = availableGiftAmount;
					}
					Log.e("shouyi", redpage + "" + availableGiftAmount);
					coupon_num.setText(df.format(redpage) + "元");
					// }

					// Log.e("shouyi", coupon_num.getText().toString());
				}
			}
		}

	}

	private void changeStatus() {
		if (li_tishi.getVisibility() == View.VISIBLE) {
			tv_min_increaseNum.setVisibility(View.INVISIBLE);
		} else {
			tv_min_increaseNum.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, intent);
		if (intent != null) {
			if (arg1 == 2) {
				ischange = arg1;

				if (intent.getBooleanExtra("red", false)) {
					use_status = true;
					flagred = true;
					reward_id = intent.getStringExtra("id");
					red_rate = Double
							.parseDouble(intent.getStringExtra("rate"));
					red_type = intent.getStringExtra("red_type");
					availableGiftAmount = Double.parseDouble(intent
							.getStringExtra("availableGiftAmount"));
					Log.e("intent", intent.getStringExtra("id"));
					Log.e("intentname", intent.getStringExtra("name") + "");
					Log.e("intentrate", intent.getStringExtra("rate") + "");
					coupon_name.setText(intent.getStringExtra("name"));
					interest();

				} else {
					use_status = false;
					reward_id = intent.getStringExtra("id");
					red_type = intent.getStringExtra("red_type");
					coupon_rate = Double.parseDouble(intent
							.getStringExtra("rate"));
					coupon_name.setText(intent.getStringExtra("name"));
					incoupon();
				}
			} else if (arg1 == 1) {
				ischange = arg1;
				flagred = false;
				reward_id = "";
				red_type = "";
				coupon_rate = 0.0;
				use_status = true;
				tishi_coupon = true;
				resultcoupon = false;
			}

		}
	}

	private void verityUser() {
		VolleryUtil vollery = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if (jsonObject.optInt("success") == 1) {
					Log.e("userinfo", jsonObject.toString());
					JSONObject json = jsonObject.optJSONObject("data");
					// availableGiftAmount =
					// json.optDouble("availableGiftAmount");
					payPass = json.optBoolean("payPass");
					Intent intent = null;
					// if (!json.optBoolean("nameAuth")
					// && !json.optBoolean("isBindCard")) {
					// intent = new Intent(V6BbtinvestActivity.this,
					// RealName.class);
					// startActivity(intent);
					// return;
					// }
					if (json.optString("bankMobileStatus") != null
							&& !json.optString("bankMobileStatus").equals("")
							&& json.optString("bankMobileStatus").equals(
									"WAIT_CHECK")) {

					}
					if (!json.optBoolean("isBindCard")) {
						intent = new Intent(V6BbtinvestActivity.this,
								V6WebviewChild.class);
						intent.putExtra(
								"url",
								Constant.URL
										+ "h5/account/bankcard/bind/view?app=true&token=");
						intent.putExtra("title", "智投宝");
						startActivity(intent);
						return;
					}

					// if (btn.getText().toString().equals("账户可用余额不足,请充值!")) {
					//
					// Intent intent5 = new Intent(V6BbtinvestActivity.this,
					// V4AddMoneySActiviy.class);
					// startActivity(intent5);
					// return;
					// }
					// if (et_money.getText().toString().equals("")) {
					// ToastUtil.showCenterToast(DetailsActivity.this,
					// "请输入投资金额");
					// return;
					// }
					if (minAmount > remainAmount) {
						// if((Integer.valueOf(et_money.getText().toString())!=remainAmount)){
						// ToastUtil.showCenterToast(DetailsActivity.this,
						// "投资金额的增量要为" + increaseNum + "的倍数");
						// return;
						// }
						// ss
					} else {
						if (addAndSubView.getNum() < minAmount) {
							ToastUtil.showCenterToast(V6BbtinvestActivity.this,
									"投资额不能小于最低起投金额");
							return;
						}
					}

					if (maxAmount != 0) {
						if (addAndSubView.getNum() > maxAmount) {
							ToastUtil.showCenterToast(V6BbtinvestActivity.this,
									"最大投资金额为" + maxAmount + "元");
							return;
						}
					}

					// if(remainAmount<increaseNum){
					//
					// }
					if (minAmount > remainAmount) {
						// if((Integer.valueOf(et_money.getText().toString())!=remainAmount)){
						// ToastUtil.showCenterToast(DetailsActivity.this,
						// "投资金额的增量要为" + increaseNum + "的倍数");
						// return;
						// }

					} else {
						if ((addAndSubView.getNum() - minAmount) % increaseNum != 0) {
							ToastUtil.showCenterToast(V6BbtinvestActivity.this,
									"投资金额的增量要为" + increaseNum + "的倍数");
							return;
						}
					}

					// if (!json.optBoolean("payPass")) {
					// intent = new Intent(V6BbtinvestActivity.this,
					// Set_tr_pw.class);
					// startActivity(intent);
					// return;
					// }
					if (!tv_title.getText().toString().contains("闪电")
							&& (coupon_name.getText().toString().equals("去使用") && tishi_coupon)) {
						tishi_coupon = false;
						li_tishi.setVisibility(View.VISIBLE);
						tv_tishi.setText("亲，您还有未使用的礼券哦!");
						Toast.makeText(V6BbtinvestActivity.this,
								"亲，您还有未使用的礼券哦!", 2000).show();
						return;
					}
					// Log.e("红包是否使用，是否是有红包，是否使用加息券",
					// flagred+""+availableGiftAmount+""+resultcoupon+"");
					// if(!flagred&&availableGiftAmount>0&&isred&&!resultcoupon&&tishi_coupon){
					// tishi_coupon=false;
					// li_tishi.setVisibility(View.VISIBLE);
					// tv_tishi.setText("亲,您还有红包未使用哦!");
					// Toast.makeText(DetailsActivity.this, "亲,您还有红包未使用哦!",
					// 2000).show();
					// return;
					// }
					// Log.e("加息券，是否使用加息券",
					// raisingratesFlag+""+resultcoupon+"");
					// if(!tv_title.getText().toString().contains("闪电")&&(coupon_name.getText().toString().equals("去使用"))&&tishi_coupon){
					// tishi_coupon=false;
					// li_tishi.setVisibility(View.VISIBLE);
					// tv_tishi.setText("亲,您还有加息券未使用哦!");
					// changeStatus();
					// // Toast.makeText(V6BbtinvestActivity.this,
					// "亲,您还有加息券未使用哦!", 2000).show();
					// return;
					// }
					/*
					 * if (!json.optBoolean("isBindCard")) { intent = new
					 * Intent(
					 * ProductDetailsActivity.this,V4BankCardActivity.class);
					 * startActivity(intent); return; }
					 */
					if (resultcoupon) {
						flagcoupon();
					} else {
						intent = new Intent(V6BbtinvestActivity.this,
								PayActivity.class);
						intent.putExtra("flagRedpack", flagred);
						intent.putExtra("rate", rate);
						intent.putExtra("qixian", day);
						intent.putExtra("minAmount", minAmount);
						intent.putExtra("isZhi", 1);
						intent.putExtra("flagFresh", "false");
						if (json.optBoolean("payPass")) {
							intent.putExtra("setPass", 0);
						} else {
							intent.putExtra("setPass", 1);
						}
						intent.putExtra("red_id", reward_id);
						intent.putExtra("productId", loanid);
						intent.putExtra("title_tpye", type);
						intent.putExtra("increaseNum", increaseNum);
						intent.putExtra("remainAmount", remainAmount);
						intent.putExtra("money", addAndSubView.getNum() + "");
						intent.putExtra("flagcoupon", resultcoupon);
						intent.putExtra("useCurrentAmountToInvest", ishuo);
						startActivityForResult(intent, 0);
						overridePendingTransition(R.anim.slide_in_from_bottom,
								R.anim.slide_out_to_bottom);
					}
					// intent = new Intent(DetailsActivity.this,
					// PayActivity.class);
					// intent.putExtra("flagRedpack", flagred);
					// intent.putExtra("rate", rate);
					// intent.putExtra("qixian", qixian);
					// intent.putExtra("minAmount", minAmount);
					// intent.putExtra("isZhi", zhitou);
					// intent.putExtra("productId", id);
					// intent.putExtra("title_tpye", type);
					// intent.putExtra("increaseNum", stepAmount);
					// intent.putExtra("remainAmount", remainAmount);
					// intent.putExtra("money", et_money.getText().toString());
					// intent.putExtra("flagcoupon", resultcoupon);
					// Log.e("加息券", flagcoupon + "");
					// if (resultcoupon) {
					// intent.putExtra("raisingratesId", raisingratesId);
					// }
					System.out.println("productdetails=rate===" + rate);
					// if (!json.optBoolean("payPass")) {
					// intent.putExtra("setPass", 1);
					// }

				} else {
					if (jsonObject.optString("comment").equals("please login")) {
						if (jsonObject.optString("comment").equals(
								"please login")) {

							Intent intent = new Intent(
									V6BbtinvestActivity.this,
									V6LoginActivity.class);
							intent.putExtra("close", 1);
							startActivity(intent);

						}
					} else {
						ToastUtil.showCenterToast(V6BbtinvestActivity.this,
								jsonObject.optString("comment"));
					}
				}
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		vollery.setHeaders("token", getToken());
		vollery.setHeaders("client", "android");
		vollery.setShow(true);
		vollery.setRequestMethod(0);
		vollery.execute(V6BbtinvestActivity.this, Constant.URL
				+ "/api/v5/user/userInfo", "");
	}

	private void incoupon() {
		if (!red_type.equals("")) {

			DecimalFormat df = new DecimalFormat("######0.00");
			double a = VoteCalculator.getVoteCalculator_month_Dou(
					Double.valueOf(addAndSubView.getNum()), coupon_rate, day);
			coupon_num.setText(df.format(a) + "元");
		}
	}

	private void product_ShouYi() {
		Log.e("shouyi", addAndSubView.getNum() + "");
		DecimalFormat df = new DecimalFormat("######0.00");
		if (repaytype.equals("EqualInstallment")) {
			// 按月等额本息
			String a = VoteCalculator.equalprincipalandinterest(new BigDecimal(
					addAndSubView.getNum() + ""),
					new BigDecimal("" + rate / 100).divide(
							new BigDecimal("12"), MathContext.DECIMAL128),
					month);
			tv_product_shouyi.setText(df.format(Double.parseDouble(a)) + "元");
		} else if (repaytype.equals("EqualPrincipal")) {
			// 按月等额本金
			String a = VoteCalculator.matchingprincipal(
					Double.valueOf(addAndSubView.getNum()), rate, month);
			tv_product_shouyi.setText(a + "元");
		} else {
			double a = VoteCalculator.getVoteCalculator_month_Dou(
					Double.parseDouble(String.valueOf(addAndSubView.getNum())),
					rate, day);
			Log.e("计息参数", addAndSubView.getNum() + "--" + rate + "--" + day);
			tv_product_shouyi.setText(df.format(a) + "元");
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (sha != null) {
			Editor editor = sha.edit();
			editor.clear();
			editor.commit();
		}
	}

	public void flagcoupon() {
		AjaxParams params = new AjaxParams();
		params.put("productId", loanid);
		params.put("amount", addAndSubView.getNum() + "");
		params.put("raisingratesId", reward_id);
		params.put("type", type);
		params.put("useCurrentAmountToInvest", ishuo + "");
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if (jsonObject.optInt("success") == 1) {
					LogUtils.e("加息券校验", jsonObject.toString());
					Intent intent = new Intent(V6BbtinvestActivity.this,
							PayActivity.class);
					// intent.putExtra("flagRedpack", flagred);
					intent.putExtra("rate", rate);
					intent.putExtra("qixian", day);
					intent.putExtra("minAmount", minAmount);
					intent.putExtra("isZhi", 0);
					intent.putExtra("productId", loanid);
					intent.putExtra("title_tpye", type);
					intent.putExtra("increaseNum", increaseNum);
					intent.putExtra("remainAmount", remainAmount);
					intent.putExtra("money", addAndSubView.getNum());
					intent.putExtra("flagcoupon", resultcoupon);
					intent.putExtra("raisingratesId", reward_id);
					intent.putExtra("useCurrentAmountToInvest", ishuo);
					startActivityForResult(intent, 1);
					overridePendingTransition(R.anim.slide_in_from_bottom,
							R.anim.slide_out_to_bottom);
				} else {
					Toast.makeText(V6BbtinvestActivity.this,
							jsonObject.optString("comment"), 2000).show();
				}
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setHeaders("deviceId", getdeviceId());
		volleryUtil.setRequestMethod(1);
		volleryUtil.setShow(true);
		volleryUtil.execute(V6BbtinvestActivity.this, Constant.URL
				+ "/api/v6/invest/invests/valid?", params.toString());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.rl_back:
			finish();
			break;
		case R.id.tv_mAgreement:
			intent = new Intent(V6BbtinvestActivity.this,
					V4WebViewActivity.class);
			intent.putExtra("url", "/landing/mAgreement?app=true&productId="
					+ loanid);
			intent.putExtra("title", "认购协议");
			intent.putExtra("share", 1);
			startActivity(intent);
			break;
		case R.id.rl_use_coupon:
			if (coupon_name.getText().toString().equals("登录查看")) {
				intent = new Intent(V6BbtinvestActivity.this,
						V6LoginActivity.class);
				intent.putExtra("close", 1);
				intent.putExtra("web", 1);
				startActivityForResult(intent, 1);
				return;
			}
			intent = new Intent(V6BbtinvestActivity.this, Investcoupon.class);
			// intent3.putExtra("rate", rate);
			// intent3.putExtra("month", month);
			// intent3.putExtra("money", et_money.getText().toString());
			// intent3.putExtra("red", availableGiftAmount);
			intent.putExtra("isnew", false + "");
			intent.putExtra("isred", false + "");
			intent.putExtra("id", loanid);
			intent.putExtra("flag", false + "");
			intent.putExtra("type", type);
			startActivityForResult(intent, 0);
			break;
		case R.id.tv_add_money:
			intent = new Intent(V6BbtinvestActivity.this, V6WebviewChild.class);
			intent.putExtra("url", Constant.URL
					+ "h5/account/recharge/view?app=true&token=");
			intent.putExtra("title", "智投宝");
			startActivity(intent);
			break;
		case R.id.btn_invest:
			if (coupon_name.getText().toString().equals("登录查看")) {
				intent = new Intent(V6BbtinvestActivity.this,
						V6LoginActivity.class);
				intent.putExtra("close", 1);
				intent.putExtra("web", 1);
				startActivityForResult(intent, 1);
				return;
			}
			verityUser();
			break;
		case R.id.rl_dexterity:
			if (check_dexterity.isChecked()) {
				img_ck_status.setVisibility(View.GONE);
				check_dexterity.setChecked(false);
			} else {
				img_ck_status.setVisibility(View.VISIBLE);
				check_dexterity.setChecked(true);
			}
			ishuo = check_dexterity.isChecked();
			myconmment();
			break;
		case R.id.check_mAgreement:
			if (check.isChecked()) {
				btn_invest.setEnabled(false);
				btn_invest.setBackgroundResource(R.drawable.btn_unclick);
				check.setChecked(false);
			} else {
				btn_invest.setEnabled(true);
				btn_invest.setBackgroundResource(R.drawable.v6button);
				check.setChecked(true);
			}
			break;

		}
	}

}
